package org.polaris.authority.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.polaris.authority.annotation.PolarisPermission;
import org.polaris.authority.domain.Role;
import org.polaris.authority.dto.RolePermissionDTO;
import org.polaris.authority.dto.SetRoleDTO;
import org.polaris.authority.query.RoleQuery;
import org.polaris.authority.service.IRoleService;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@Api(value = "角色管理")
@RequestMapping("/role")
@PolarisPermission(name="角色管理",descs = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "查询全部")
    @GetMapping
    @PolarisPermission(name="查询全部",descs = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Role> roles = roleService.selectAll();
            return AjaxResult.result().setObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID查询")
    @PolarisPermission(name="通过ID查询",descs = "通过ID查询")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.result().setObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID删除")
    @PolarisPermission(name="通过ID删除",descs = "通过ID删除")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            roleService.deleteById(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "新增&修改,存在ID则修改反之则新增")
    @PutMapping
    @PolarisPermission(name="保存",descs = "新增&修改,存在ID则修改反之则新增")
    public AjaxResult save(@RequestBody Role role){
        Role name = roleService.selectByName(role.getName());
        Role sn = roleService.selectBySn(role.getSn());
        if (StringUtils.isEmpty(role.getId())){
            try {

                if (Objects.nonNull(name)){
                    throw new RuntimeException("名称已存在");
                }
                if (Objects.nonNull(sn)){
                    throw new RuntimeException("唯一标识已存在");
                }
                roleService.add(role);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }

        }else {
            try {
                if (Objects.nonNull(name) && !name.getId().equals(role.getId())){
                    throw new RuntimeException("名称已存在");
                }
                if (Objects.nonNull(sn) && !sn.getId().equals(role.getId())){
                    throw new RuntimeException("唯一标识已存在");
                }
                roleService.update(role);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }
        }
    }
    @ApiOperation(value = "分页")
    @PostMapping
    @PolarisPermission(name="分页",descs = "分页")
    public AjaxResult paging(@RequestBody RoleQuery roleQuery){
        try {
            PageResult paging = roleService.paging(roleQuery);
            System.out.println(paging);
            return AjaxResult.result().setObj(paging);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }

    }


    @PutMapping("/permission")
    @PolarisPermission(name = "设置权限")
    public AjaxResult setRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.setRolePermission(dto);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @GetMapping("/permissionSns/{roleId}")
    @PolarisPermission(name = "获取角色的权限标识")
    public AjaxResult getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return AjaxResult.result().setObj(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("获取角色的权限标识失败!"+e.getMessage());
        }
    }

    @PutMapping("/saveRoles")
    @PolarisPermission(name = "设置角色")
    public AjaxResult setRoles(@RequestBody SetRoleDTO dto){
        try {
            System.out.println(dto);
            roleService.setRoles(dto);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("设置角色失败!"+e.getMessage());
        }
    }

    @GetMapping("/getRoles/{eId}")
    @PolarisPermission(name = "获取用户角色")
    public AjaxResult getEmpRoles(@PathVariable("eId")Long eId){
        try {
            List<Long> roles = roleService.getEmpRoles(eId);
            return AjaxResult.result().setObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("获取用户角色失败!"+e.getMessage());
        }
    }

}
